import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/login/login.vue'
// import Article from '@/views/article/article.vue'
// import NotFound from '@/views/NotFound/NotFound.vue'
import Layout from '@/views/layout/layout.vue'
import Dashboard from '@/views/dashboard/dashboard.vue'
import store from '@/store'

const Login = () => import('@/views/login/login.vue')
const Article = () => import('@/views/article/article.vue')
const NotFound = () => import('@/views/NotFound/NotFound.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      { path: 'dashboard', component: Dashboard },
      { path: 'article', component: Article }
    ]
  },
  { path: '*', component: NotFound }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})

export default router
